using System.Collections.Generic;
using UnityEngine;

public class NavigationByPoints : MonoBehaviour
{
	protected struct GameObjectParent
	{
		public GameObject gameObject;

		public Transform parent;

		public GameObjectParent(GameObject gameObject, Transform parent)
		{
			this.gameObject = gameObject;
			this.parent = parent;
		}
	}

	public int key;

	protected List<GameObjectParent> gameObjectParent = new List<GameObjectParent>();

	protected Transform tr;

	protected int _prevPhotonTime;

	protected int _prevInterPhotonTime;

	protected float _moveTime;

	protected float waitTime;

	protected virtual void Start()
	{
		if (SelfMovingObjectManager.instance != null)
		{
			SelfMovingObjectManager.instance.Add(key, base.gameObject.transform);
		}
		tr = base.transform;
	}

	protected virtual void FixedUpdate()
	{
		waitTime -= Time.fixedDeltaTime;
		if (waitTime > 0f)
		{
			return;
		}
		float num;
		if (settings.offlineMode)
		{
			num = Time.time % _moveTime;
		}
		else
		{
			int num2 = (int)(PhotonNetwork.time * 1000.0);
			if (_prevPhotonTime == num2)
			{
				_prevInterPhotonTime += (int)(Time.deltaTime * 1000f);
				num2 = _prevInterPhotonTime;
			}
			else
			{
				_prevPhotonTime = num2;
				_prevInterPhotonTime = num2;
			}
			num = (float)num2 % (_moveTime * 1000f) / 1000f;
		}
		if (num < 0f)
		{
			num += _moveTime;
		}
		if (tr == null)
		{
			tr = base.transform;
		}
		base.transform.localPosition = CalcPositiion(Mathf.Abs(num));
		if (GameController.instance.playerScript == null || Vector3.Distance(GameController.instance.playerScript.transform.position, tr.position) > 200f)
		{
			waitTime = 5f;
		}
	}

	protected virtual Vector3 CalcPositiion(float value)
	{
		return base.transform.localPosition;
	}

	public void Enter(GameObject inGameObject)
	{
		for (int i = 0; i < gameObjectParent.Count; i++)
		{
			if (gameObjectParent[i].gameObject == inGameObject)
			{
				return;
			}
		}
		gameObjectParent.Add(new GameObjectParent(inGameObject, inGameObject.transform.parent));
		inGameObject.transform.parent = base.transform;
		PlayerTransformPhoton component = inGameObject.GetComponent<PlayerTransformPhoton>();
		if (component != null)
		{
			component.inParom = true;
			component.parentGameObjectTransform = base.gameObject.transform;
			component.parentGameObjectKey = key;
		}
	}

	public void Leave(GameObject outGameObject)
	{
		int i;
		for (i = 0; i < gameObjectParent.Count; i++)
		{
			if (gameObjectParent[i].gameObject == outGameObject)
			{
				outGameObject.transform.parent = gameObjectParent[i].parent;
				break;
			}
		}
		if (i < gameObjectParent.Count)
		{
			gameObjectParent.RemoveAt(i);
		}
		PlayerTransformPhoton component = outGameObject.GetComponent<PlayerTransformPhoton>();
		if (component != null)
		{
			component.inParom = false;
		}
	}

	protected void OnTriggerEnter(Collider collider)
	{
		string text = collider.gameObject.tag;
		if (text.Equals("Car"))
		{
			Enter(collider.gameObject);
		}
	}

	protected void OnTriggerExit(Collider collider)
	{
		string text = collider.gameObject.tag;
		if (text.Equals("Car"))
		{
			Leave(collider.gameObject);
		}
	}
}
